/** 
 * @(#)NetworkDemo
 */

/**
 * @author Robert Denda&Didier Gutacker
 * @version 1.1
 * @see Demo
 * @see Network
 * @see Link
 * @see Node
 */

/**
 *
 * original author Robert Denda, changes made by Didier Gutacker
 */

public class NetworkDemo extends Demo {
  Network net;

  public NetworkDemo( Network net ) {
    this.net = net;
    this.running = false;
    t = new Thread( net.getThreadGroup(), this );
    t.setPriority( Thread.MIN_PRIORITY );
    infoWindow = new InfoWindow( new String( "Multicast Routing Demo" ), 20, 70 );
  } // NetworkDemo.NetworkDemo

  public void run() {
    infoWindow.expose( net.getSimulation().getText().demoGreetings );
    infoWindow.waitForConfirmation();
    Node nodeB = net.addNode( 200, 10 );
    Node nodeC = net.addNode( 400, 10 );
    Link linkBC = net.addLink( nodeB, nodeC );
    delay( 2 );
    infoWindow.expose( net.getSimulation().getText().demoPart1 );
    infoWindow.waitForConfirmation();
    Node nodeA = net.addNode( 10, 200 );
    Node nodeD = net.addNode( 400, 200 );
    Node nodeE = net.addNode( 600, 100 );
    Link linkAB = net.addLink( nodeA, nodeB );
    delay( 2 );
    Link linkAD = net.addLink( nodeA, nodeD );
    Link linkCD = net.addLink( nodeC, nodeD );
    delay( 2 );
    infoWindow.expose( net.getSimulation().getText().demoPart2 );
    infoWindow.waitForConfirmation();
    net.inspectRoutingTable( nodeC );
    delay( 1 );
    Link linkCE = net.addLink( nodeC, nodeE );
    delay( 2 );
    net.inspectRoutingTable( nodeC );
    delay( 1 );
    infoWindow.expose( net.getSimulation().getText().demoPart3 );
    infoWindow.waitForConfirmation();
    net.startSending( nodeA );
    delay( 3 );
    infoWindow.expose( net.getSimulation().getText().demoPart4 );
    infoWindow.waitForConfirmation();
    delay( 1 );
    Link linkDE = net.addLink( nodeD, nodeE );
    delay( 6 );
    infoWindow.expose( net.getSimulation().getText().demoPart5 );
    infoWindow.waitForConfirmation();
    delay( 1 );
    net.sendPruningMessage( nodeE );
    delay( 3 );
    net.sendPruningMessage( nodeD );
    delay( 5 );
    infoWindow.expose( net.getSimulation().getText().demoPart6 );
    infoWindow.waitForConfirmation();
    delay( 1 );
    net.sendGraftingMessage( nodeE );
    delay( 3 );
    net.sendGraftingMessage( nodeD );
    delay( 3 );
    infoWindow.expose( net.getSimulation().getText().demoEnd );
    infoWindow.waitForConfirmation();
    running = false;
    net.stopDemo();
  } // NetworkDemo.run

  final void delay() {
    int speed = net.getSpeed();
    int duration;
    if ( speed == 0 ) {
      duration = 15000;
    } else if ( speed == 1 ) {
      duration = 8000;
    } else {
      duration = 4000;
    } // if
    try {
      Thread.sleep( duration );
    } catch( InterruptedException e ) {
    } // try
  } // NetworkDemo.delay

  final void delay( int times ) {
    for ( int i = 0; i < times; i += 1 ) {
      delay();
    } // for
  } // NetworkDemo.delay

} // NetworkDemo
